package by.yakusekmaxim.bookgid.webapp.page.home.book;

import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.Feedback;
import by.yakusekmaxim.bookgid.datamodel.Review;
import by.yakusekmaxim.bookgid.datamodel.UserAccount;
import by.yakusekmaxim.bookgid.services.FeedbackService;
import by.yakusekmaxim.bookgid.services.ReviewService;

public class ReviewListPanel extends Panel {

	@Inject
	private ReviewService reviewService;

	@Inject
	FeedbackService feedbackService;

	private Feedback feedback;
	
	public ReviewListPanel(String id, final Book book) {
		super(id);

		List<Review> listReview = reviewService.getAllReview(book, true);

		final ListView reviewList = new ListView<Review>("review", listReview) {
			@Override
			protected void populateItem(ListItem<Review> item) {
				final Review review = item.getModelObject();
				UserAccount user = review.getUserAccount();
				feedback = feedbackService.getBookUserEvaluation(book,
						user);

				item.add(new Label("username", user.getFirstName() + " "
						+ user.getLastName()));

				item.add(new Label("text", review.getText()));
				
				item.add(new Label("date", review.getDate()));

				Label feedbackLabel = new Label("feedbackLabel", "");
				if (feedback != null) {
					System.out.println(feedback.getRating());
					feedbackLabel.setDefaultModelObject(new ResourceModel(
							"p.home.ReviewListPanel.feedback").getObject()
							+ feedback.getRating());
				}

				item.add(feedbackLabel);
			};
		};
		add(reviewList);
	};

}
